package com.xinkao.log.util;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

import java.text.SimpleDateFormat;

/**
 * @author ZSX
 * @Description
 * @createTime 2021/1/11 17:27
 */
public class ExcelUtils {

    /**
     * @Author ZSX
     * @Description 判断导入数据类型xlsx
     * @Date 2020/12/21 10:30
     * @Param [cell]
     * @return String
     **/
    public static String getCellType(Cell cell){
        String cellValue = "";
        if(null == cell){
            return cellValue;
        }
        switch (cell.getCellTypeEnum()){
            case NUMERIC:
                String data = cell.getCellStyle().getDataFormatString();
                String format1="m/d/yy";
                String format2="yyyy\\-mm\\-dd";
                String format3="yyyy/MM/dd";
                if (format1.equals(data) || format2.equals(data) || format3.equals(data)) {
                    cellValue = new SimpleDateFormat("yyyy/MM/dd").format(cell.getDateCellValue());
                } else {
                    cellValue = String.valueOf((long)cell.getNumericCellValue());
                }
                break;
            case STRING:
                cellValue = cell.getStringCellValue();
                break;
            case BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case FORMULA:
                cellValue = String.valueOf(cell.getCellFormula());
                break;
            case BLANK:
                break;
            case ERROR:
                break;
            default:
                cellValue = "未知类型";
                break;
        }
        return cellValue;
    }

    public static String getCellTypeXls(HSSFCell cell){
        String cellValue = "";
        if(null == cell){
            return cellValue;
        }
        if(cell.getCellTypeEnum() == CellType.STRING){
            cellValue = cell.getStringCellValue().trim();
        }
        if(cell.getCellTypeEnum() == CellType.NUMERIC){
            SimpleDateFormat df = new SimpleDateFormat();
            cellValue = df.format(cell.getNumericCellValue()).toString();
        }
        if(cell.getCellTypeEnum() == CellType.BOOLEAN){
            cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
        }
        if(cell.getCellTypeEnum() == CellType.FORMULA){
            cellValue = cell.getCellFormula();
        }
        if(cell.getCellTypeEnum() == CellType._NONE){
            cellValue = "未知类型";
        }
        if(cell.getCellTypeEnum() == CellType.BLANK){
            cellValue = "";
        }

        return cellValue;
    }
}
